/*
 * Copyright 2009-2010 the original author or authors.
 *
 * Licensed under the Apache license, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.internna.iwebjtracker.spring.mvc;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.internna.iwebjtracker.administration.PollManager;
import org.internna.iwebmvc.model.Role;
import org.internna.iwebmvc.dao.SecurityDAO;
import org.internna.iwebjtracker.model.TrackerUser;
import org.internna.iwebjtracker.server.ServerManager;
import org.internna.iwebjtracker.services.ActivityReporter;
import org.internna.iwebjtracker.services.TrackerUserManager;
import org.internna.iwebmvc.spring.mvc.interceptors.ModelDecoratorInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;

/**
 * Adds the ServerManager instance to the collection of objects available to the views.
 *
 * @author Jose Noheda
 * @since 1.0
 */
public final class IWebJTrackerModelDecoratorInterceptor extends ModelDecoratorInterceptor {

    /**
     * JSP key to retrieve all the authorities (roles) in the system.
     */
    public static final String AUTHORITIES = "authorities";

    /**
     * JSP key to retrieve the current logged in user.
     */
    public static final String TRACKER_USER = "trackerUser";

    /**
     * JSP key to retrieve the current active polls.
     */
    public static final String POLL_MANAGER = "pollManager";

    /**
     * JSP key to retrieve the server manager.
     */
    public static final String SERVER_MANAGER = "serverManager";

    /**
     * JSP key to retrieve user/system statistics.
     */
    public static final String ACTIVITY_REPORTER = "activityReporter";

    @Autowired private SecurityDAO dao;
    @Autowired private PollManager pollManager;
    @Autowired private ServerManager serverManager;
    @Autowired private ActivityReporter activityReporter;
    @Autowired private TrackerUserManager trackerUserManager;

    /**
    * Retrieves the model and if exists adds the configured serverManager.
    */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
        if (modelAndView != null) {
            ModelMap model = modelAndView.getModelMap();
            if (model != null) {
                addAttributes(model);
            }
        }
    }

    protected void addAttributes(ModelMap model) {
        model.addAttribute(SERVER_MANAGER, serverManager);
        model.addAttribute(ACTIVITY_REPORTER, activityReporter);
        List<Role> roles = dao.findAuthorities();
        TrackerUser trackerUser = trackerUserManager.getCurrentTrackerUser();
        model.addAttribute(AUTHORITIES, roles);
        model.addAttribute(POLL_MANAGER, pollManager);
        model.addAttribute(TRACKER_USER, trackerUser);
        if (logger.isDebugEnabled()) {
            logger.debug("Populating view with [" + AUTHORITIES + "=" + roles + "]");
            logger.debug("Populating view with [" + POLL_MANAGER + "=" + pollManager + "]");
            logger.debug("Populating view with [" + TRACKER_USER + "=" + trackerUser + "]");
        }
    }

}
